package sy.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.Bug;
import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.Project;
import sy.service.ProjectServiceI;

@Controller
@RequestMapping("/prjController")
public class PrjController {

	private ProjectServiceI projectService;

	public ProjectServiceI getProjectService() {
		return projectService;
	}

	@Autowired
	public void setProjectService(ProjectServiceI projectService) {
		this.projectService = projectService;
	}

	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(Project prj) throws Exception {
		DataGrid dg = null;
		try {
			dg = projectService.datagrid(prj);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return dg;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(Project prj) {
		Json j = new Json();
		try {
			Project r = projectService.save(prj);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(r);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/remove")
	@ResponseBody
	public Json remove(Project prj) {
		projectService.remove(prj.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		return j;
	}

	@RequestMapping("/prjglEdit")
	public String prjglEdit(String id, HttpServletRequest request) {
		Project prj = projectService.getProject(id);
		request.setAttribute("prj", prj);
		//重定向到JSP页面
		return "automation/project/gcglEdit";
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Project prj) {
		Json j = new Json();
		try {
			Project r = projectService.edit(prj);
			j.setSuccess(true);
			j.setMsg("保存成功！");
			j.setObj(r);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

}
